'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url agent/setting/kh/RebackPercent 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			type,
			huichuan,
			tfid
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		let request_id = vk.pubfn.getUniCloudRequestId();
		let look_log = await db.collection('vk-test').add({
			tips: "回传配置进行了修改日志云函数",
			request_id: request_id,
		})
		if (type == "get") {
			res.RebackPercent = await db.collection('a_config').where({
				name: "RebackPercent",
				user_id: userInfo._id
			}).get()

			//不存在数据时,自动添加一条
			if (res.RebackPercent.data.length == 0) {
				let adds = await db.collection('a_config').add({
					name: "RebackPercent",
					user_id: userInfo._id,
					douyinRadio: 0,
					kuaishouRadio: 0,
					tengxunRadio: 0,
					ucRadio: 0,
					baiduRadio: 0,
					oppoRadio: 0,
					vivoRadio: 0,
					douyin_ReturnRatio: 0,
					douyin_UnReturnRatio: 0,
					kuaishou_ReturnRatio: 0,
					kuaishou_UnReturnRatio: 0,
					tengxun_ReturnRatio: 0,
					tengxun_UnReturnRatio: 0,
					uc_ReturnRatio: 0,
					uc_UnReturnRatio: 0,
					baidu_ReturnRatio: 0,
					baidu_UnReturnRatio: 0,
					oppo_ReturnRatio: 0,
					oppo_UnReturnRatio: 0,
					vivo_ReturnRatio: 0,
					vivo_UnReturnRatio: 0,
					firstorder: true,
					newuser: true,
					douyin: 100,
					douyin_url: "",
					douyin_token: "",
					kuaishou: 100,
					uc: 100,
					tengxun: 100,
					baidu: 100,
					baidu_token: "",
					oppo: 100,
					douyinMinPrice: 0,
					douyinMaxPrice: 99999,
					kuaishouMinPrice: 0,
					kuaishouMaxPrice: 99999,
					baiduMinPrice: 0,
					baiduMaxPrice: 99999,
					ucMinPrice: 0,
					tengxunMinPrice: 0,
					tengxunMaxPrice: 99999,
					oppoMinPrice: 0,
					oppoMaxPrice: 99999,
					oppo_owner_id: "",
					oppo_api_id: "",
					oppo_api_key: "",
					hour_backhaul: 24,
					vivo: 100,
					vivoMinPrice: 0,
					vivoMaxPrice: 99999,
					vivo_advertiser_id: "",
					access_token_vivo: "",
					vivo_srcId: ""
				})
			}
			return res;
		}

		if (tfid && type == "save") {
			let update = await db.collection('a_config').where({
				name: "RebackPercent",
				tfid: tfid
			}).update({
				douyinRadio: huichuan.douyinRadio * 1 || 0,
				kuaishouRadio: huichuan.kuaishouRadio || 0,
				tengxunRadio: huichuan.tengxunRadio || 0,
				ucRadio: huichuan.ucRadio || 0,
				baiduRadio: huichuan.baiduRadio || 0,
				oppoRadio: huichuan.oppoRadio || 0,
				vivoRadio: huichuan.vivoRadio || 0,
				douyin_ReturnRatio: huichuan.douyin_ReturnRatio || 0,
				douyin_UnReturnRatio: huichuan.douyin_UnReturnRatio || 0,
				kuaishou_ReturnRatio: huichuan.kuaishou_ReturnRatio || 0,
				kuaishou_UnReturnRatio: huichuan.kuaishou_UnReturnRatio || 0,
				tengxun_ReturnRatio: huichuan.tengxun_ReturnRatio || 0,
				tengxun_UnReturnRatio: huichuan.tengxun_UnReturnRatio || 0,
				uc_ReturnRatio: huichuan.uc_ReturnRatio || 0,
				uc_UnReturnRatio: huichuan.uc_UnReturnRatio || 0,
				baidu_ReturnRatio: huichuan.baidu_ReturnRatio || 0,
				baidu_UnReturnRatio: huichuan.baidu_UnReturnRatio || 0,
				oppo_ReturnRatio: huichuan.oppo_ReturnRatio || 0,
				oppo_UnReturnRatio: huichuan.oppo_UnReturnRatio || 0,
				vivo_ReturnRatio: huichuan.vivo_ReturnRatio || 0,
				vivo_UnReturnRatio: huichuan.vivo_UnReturnRatio || 0,
				newuser: huichuan.newuser || 0,
				firstorder: huichuan.firstorder || 0,
				douyin: huichuan.douyin * 1 || 0,
				douyin_url: huichuan.douyin_url || 0,
				douyin_token: huichuan.douyin_token || 0,
				kuaishou: huichuan.kuaishou * 1 || 0,
				baidu: huichuan.baidu * 1 || 0,
				tengxun: huichuan.tengxun * 1 || 0,
				uc: huichuan.uc * 1 || 0,
				baidu_token: huichuan.baidu_token || 0,
				oppo: huichuan.oppo * 1 || 0,
				oppo_owner_id: huichuan.oppo_owner_id || 0,
				oppo_api_id: huichuan.oppo_api_id || 0,
				oppo_api_key: huichuan.oppo_api_key || 0,
				douyinMinPrice: huichuan.douyinMinPrice * 1 || 0,
				douyinMaxPrice: huichuan.douyinMaxPrice * 1 || 0,
				kuaishouMinPrice: huichuan.kuaishouMinPrice * 1 || 0,
				kuaishouMaxPrice: huichuan.kuaishouMaxPrice * 1 || 0,
				baiduMinPrice: huichuan.baiduMinPrice * 1 || 0,
				baiduMaxPrice: huichuan.baiduMaxPrice * 1 || 0,
				ucMinPrice: huichuan.ucMinPrice || 0,
				tengxunMinPrice: huichuan.tengxunMinPrice * 1 || 0,
				tengxunMaxPrice: huichuan.tengxunMaxPrice * 1 || 0,
				oppoMinPrice: huichuan.oppoMinPrice * 1 || 0,
				oppoMaxPrice: huichuan.oppoMaxPrice * 1 || 0,
				hour_backhaul: huichuan.hour_backhaul * 1 || 0,
				vivo: huichuan.vivo * 1 || 0,
				vivoMinPrice: huichuan.vivoMinPrice * 1 || 0,
				vivoMaxPrice: huichuan.vivoMaxPrice * 1 || 0,
				vivo_advertiser_id: huichuan.vivo_advertiser_id || 0,
				access_token_vivo: huichuan.access_token_vivo || 0,
				vivo_srcId: huichuan.vivo_srcId || 0,
				last_time:new Date().getTime()
			})
			return res;
		}

		if (type == "save" && !tfid) {
			let update = await db.collection('a_config').where({
				name: "RebackPercent",
				user_id: userInfo._id
			}).update({
				
				douyinRadio: huichuan.douyinRadio || 0,
				kuaishouRadio: huichuan.kuaishouRadio || 0,
				tengxunRadio: huichuan.tengxunRadio || 0,
				ucRadio: huichuan.ucRadio || 0,
				baiduRadio: huichuan.baiduRadio || 0,
				oppoRadio: huichuan.oppoRadio || 0,
				vivoRadio: huichuan.vivoRadio || 0,
				douyin_ReturnRatio: huichuan.douyin_ReturnRatio || 0,
				douyin_UnReturnRatio: huichuan.douyin_UnReturnRatio || 0,
				kuaishou_ReturnRatio: huichuan.kuaishou_ReturnRatio || 0,
				kuaishou_UnReturnRatio: huichuan.kuaishou_UnReturnRatio || 0,
				tengxun_ReturnRatio: huichuan.tengxun_ReturnRatio || 0,
				tengxun_UnReturnRatio: huichuan.tengxun_UnReturnRatio || 0,
				uc_ReturnRatio: huichuan.uc_ReturnRatio || 0,
				uc_UnReturnRatio: huichuan.uc_UnReturnRatio || 0,
				baidu_ReturnRatio: huichuan.baidu_ReturnRatio || 0,
				baidu_UnReturnRatio: huichuan.baidu_UnReturnRatio || 0,
				oppo_ReturnRatio: huichuan.oppo_ReturnRatio || 0,
				oppo_UnReturnRatio: huichuan.oppo_UnReturnRatio || 0,
				vivo_ReturnRatio: huichuan.vivo_ReturnRatio || 0,
				vivo_UnReturnRatio: huichuan.vivo_UnReturnRatio || 0,
				newuser: huichuan.newuser || 0,
				firstorder: huichuan.firstorder || 0,
				douyin: huichuan.douyin * 1 || 0,
				douyin_url: huichuan.douyin_url || 0,
				douyin_token: huichuan.douyin_token || 0,
				kuaishou: huichuan.kuaishou * 1 || 0,
				baidu: huichuan.baidu * 1 || 0,
				tengxun: huichuan.tengxun * 1 || 0,
				uc: huichuan.uc * 1 || 0,
				baidu_token: huichuan.baidu_token || 0,
				oppo: huichuan.oppo * 1 || 0,
				oppo_owner_id: huichuan.oppo_owner_id || 0,
				oppo_api_id: huichuan.oppo_api_id || 0,
				oppo_api_key: huichuan.oppo_api_key || 0,
				douyinMinPrice: huichuan.douyinMinPrice || 0,
				douyinMaxPrice: huichuan.douyinMaxPrice || 0,
				kuaishouMinPrice: huichuan.kuaishouMinPrice || 0,
				kuaishouMaxPrice: huichuan.kuaishouMaxPrice || 0,
				baiduMinPrice: huichuan.baiduMinPrice || 0,
				baiduMaxPrice: huichuan.baiduMaxPrice || 0,
				ucMinPrice: huichuan.ucMinPrice || 0,
				tengxunMinPrice: huichuan.tengxunMinPrice || 0,
				tengxunMaxPrice: huichuan.tengxunMaxPrice || 0,
				oppoMinPrice: huichuan.oppoMinPrice * 1 || 0,
				oppoMaxPrice: huichuan.oppoMaxPrice * 1 || 0,
				hour_backhaul: huichuan.hour_backhaul * 1 || 0,
				vivo: huichuan.vivo * 1 || 0,
				vivoMinPrice: huichuan.vivoMinPrice * 1 || 0,
				vivoMaxPrice: huichuan.vivoMaxPrice * 1 || 0,
				vivo_advertiser_id: huichuan.vivo_advertiser_id || 0,
				access_token_vivo: huichuan.access_token_vivo || 0,
				vivo_srcId: huichuan.vivo_srcId || 0
			})
		}

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}